package com.xh.petdogexpert.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xh.petdogexpert.base.BaseController;
import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.base.ResponseData;
import com.xh.petdogexpert.sys.dto.LovItem;
import com.xh.petdogexpert.sys.mapper.LovItemMapper;
import com.xh.petdogexpert.sys.service.ILovItemService;

@RequestMapping(value = "sys/")
@Controller
public class LovItemController extends BaseController{

	@Autowired
	ILovItemService lovService;

	@Autowired
	LovItemMapper lovMapper;

	final static Logger logger = LoggerFactory.getLogger(LovItemController.class);

	@RequestMapping(value = "lovItem/queryLovItems")
	@ResponseBody
	public ResponseData queryLovItems(LovItem lovItem) {

		ResponseData rData = new ResponseData();
		List<LovItem> lovItems = lovService.queryLovItems(lovItem);
		rData.setData(lovItems);
		return rData;
	}

	@RequestMapping(value = "lovItem/getLovItem")
	@ResponseBody
	public ResponseData getLovItem(LovItem lovItem) throws BaseException {

		ResponseData rData = new ResponseData();
		LovItem rLov = null;

		rLov = lovService.getLovItem(lovItem);
		rData.setData(rLov);

		return rData;
	}

	@RequestMapping(value = "lovItem/addUpdateLovItem")
	@ResponseBody
	public ResponseData addUpdateLovItem(String lovsItemStr) throws BaseException {

		ObjectMapper objectMapper = new ObjectMapper();
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, LovItem.class);
		List<LovItem> lovItems;
		System.out.println("lovsItemStr " + lovsItemStr);
		try {
			lovItems = objectMapper.readValue(lovsItemStr, javaType);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("转换出错");
		} 
		
		logger.debug(lovItems + "   " + lovItems.size() + "!!!!!!!! !!!!!!!!!");

		ResponseData rData = new ResponseData();

		lovService.addUpdateLovItem(lovItems);

		rData.setMsg("保存成功");

		return rData;
	}

	@RequestMapping(value = "lovItem/deleteLovItem")
	@ResponseBody
	public ResponseData deleteLovItem(LovItem lovItem) throws BaseException {

		ResponseData rData = new ResponseData();

		int count = lovService.deleteLovItem(lovItem);
		rData.setData(count);
		rData.setMsg("删除成功");
		return rData;
	}
}
